package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dao.DishDTO;
import com.sky.dao.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.entity.SetmealDishes;
import com.sky.exception.CanDeleteException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import com.sky.vo.DishWithFlavorVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Dish> listByCategoryId(Long categoryId) {

       List<Dish> list = new ArrayList<>();
        Dish dish = new Dish();
        dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        list = dishMapper.list(dish);

        return list;
    }

    @Override
    public PageResult page(DishPageDTO dishPageDTO) {
        PageHelper.startPage(dishPageDTO.getPage(), dishPageDTO.getPageSize());
        Page<DishVo> page = dishMapper.page(dishPageDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getResult());
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public DishWithFlavorVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        String categoryName = categoryMapper.getNameById(dish.getCategoryId());
        List<DishFlavor> flavors = dishFlavorMapper.getById(id);
        DishWithFlavorVO dishWithFlavorVO = new DishWithFlavorVO();
        BeanUtils.copyProperties(dish, dishWithFlavorVO);
        dishWithFlavorVO.setFlavors(flavors);
        dishWithFlavorVO.setCategoryName(categoryName);
        return dishWithFlavorVO;
    }

    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            dishFlavorMapper.deleteByDishId(dishId);
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if (status == StatusConstant.ENABLE) {
                throw new CanDeleteException(MessageConstant.CAN_NOT_DELETE_SALE_PRODUCT);
            }
            ArrayList<SetmealDishes> selectedDishes = setmealDishMapper.getBySetmealId(id);
            if (!(selectedDishes == null || selectedDishes.size() == 0)) {
                throw new CanDeleteException(MessageConstant.CAN_NOT_DELETE_PACKAGE_PRODUCT);
            }

            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public List<DishWithFlavorVO> listWithFlavor(Long categoryId) {
        ArrayList<DishWithFlavorVO> list = new ArrayList<>();
        List<Dish> dishes = listByCategoryId(categoryId);
        for (Dish dish : dishes) {
            DishWithFlavorVO dishWithFlavorVO = new DishWithFlavorVO();
            BeanUtils.copyProperties(dish, dishWithFlavorVO);
            List<DishFlavor> flavors = dishFlavorMapper.getById(dish.getId());
            dishWithFlavorVO.setFlavors(flavors);
            list.add(dishWithFlavorVO);
        }
        return list;
    }


}
